home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / clang / netclb23.zip / EXAMPLES.EXE / IPXCHAT.C < prev    next >
C/C++ Source or Header  |  1994-05-20  |  5KB  |  179 lines

  1. /******************************* IPXCHAT ********************************/
  2. /* This example program will chat between two stations sending a single */
  3. /* character at a time in either direction.                             */
  4. /************************************************************************/
  5.  
  6. #define ESCAPE_KEY  0x1b
  7. #define RETURN_KEY  0x0d
  8.  
  9. #define SHORT_LIVED 0x00
  10. #define SOCKET      0x4848        /* Socket number we will use */
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <dos.h>
  15. #include <conio.h>
  16. #include <memory.h>
  17.  
  18. #include "netware.h"
  19.  
  20. word program_socket ;
  21. word server_socket ;
  22.  
  23. EVENT_CONTROL_BLOCK receiveECB ;
  24. EVENT_CONTROL_BLOCK sendECB ;
  25. IPX_HEADER          IPX_receive_header ;
  26. IPX_HEADER          IPX_send_header ;
  27.  
  28. char packet_data;
  29.  
  30. word connection_number;
  31.  
  32. /********************** function prototypes ********************/
  33.  
  34. word SetupForReceive( void );
  35. void SetupForSend( int connection_number );
  36. void TransmitReceive( void );
  37.  
  38. /***************************** main() *************************/
  39.  
  40. void main( int argc, char *argv[] )
  41. {
  42. int rc;
  43.  
  44.     if (argc != 2)
  45.     {
  46.        printf("Invalid number of paramaters\n");
  47.        printf("Usage is:\n");
  48.        printf("         ipxchat <connection_number>\n");
  49.        exit(255);
  50.     }
  51.     else
  52.        connection_number = atoi(argv[1]);
  53.        
  54.     if (IPXInitialise() != 0)
  55.     {
  56.        printf("IPX is not installed........\n");
  57.        exit(255);
  58.     }
  59.     else
  60.        printf("IPX Initialised.......\n");
  61.     program_socket = SOCKET;
  62.     if (IPXOpenSocket( &program_socket , SHORT_LIVED ) != 0)
  63.     {
  64.        printf("Failed to open socket %04.4X\n",SOCKET);
  65.        exit(255);
  66.     }
  67.     else
  68.        printf("Socket %04.4X opened\n",SOCKET);
  69.     if ( (rc = SetupForReceive()) != 0)
  70.        printf("Failed to create listen ECB: %d\n",rc);
  71.     else
  72.     {
  73.        printf("Receive ECB created\n");
  74.        SetupForSend(connection_number) ;
  75.        TransmitReceive() ;
  76.     }
  77.     IPXCloseSocket(program_socket) ;
  78. }
  79.  
  80. /********************** SetupForReceive ***********************/
  81. word SetupForReceive( void )
  82. {
  83.    memset( (void *)&receiveECB,0,sizeof(EVENT_CONTROL_BLOCK));
  84.  
  85.    NWintconvert( program_socket, &(receiveECB.socket_number) );
  86.  
  87.    receiveECB.fragment_count      = 2 ;
  88.    receiveECB.esr                 = (void (_far *) () ) 0 ;
  89.  
  90.    receiveECB.fragment[0].address = (void *)&IPX_receive_header;
  91.    receiveECB.fragment[0].length  = sizeof(IPX_receive_header);
  92.  
  93.    receiveECB.fragment[1].address = (void *)&packet_data;
  94.    receiveECB.fragment[1].length  = 1;
  95.  
  96.    return( IPXListenForPacket( &receiveECB ) );
  97. }
  98.  
  99. /*********************** SetupForSend *************************/
  100. void SetupForSend( int connection_number )
  101. {
  102. word transport_time;
  103.  
  104.     GetInternetAddress( connection_number,
  105.                         IPX_send_header.dest_addr.ina.network_number,
  106.                         IPX_send_header.dest_addr.ina.node_address,
  107.                         &server_socket);
  108.     IPX_send_header.packet_type = 4 ;
  109.     NWintconvert(program_socket,&IPX_send_header.dest_addr.socket_number);
  110.  
  111.     memset( (void *)&sendECB,0,sizeof(EVENT_CONTROL_BLOCK));
  112.  
  113.     IPXGetLocalTarget(&IPX_send_header.dest_addr.ina,
  114.                       sendECB.immediate_address,&transport_time);
  115.     NWintconvert( program_socket, &(sendECB.socket_number) );
  116.  
  117.     sendECB.fragment_count = 2;
  118.     sendECB.esr            = (void (far *) () ) 0 ;
  119.  
  120.     sendECB.fragment[0].address = (void *)&IPX_send_header;
  121.     sendECB.fragment[0].length  = sizeof(IPX_send_header);
  122.  
  123.     sendECB.fragment[1].address = (void *)&packet_data;
  124.     sendECB.fragment[1].length  = 1;
  125. }
  126.  
  127. /******************** TransmitReceive ************************/
  128. void TransmitReceive( void )
  129. {
  130.     int done;
  131.  
  132.     system("cls") ;
  133.     printf("Enter character to give to other side.\n");
  134.     printf("(Type ESCAPE to exit.)\n");
  135.  
  136.     done = 0;
  137.  
  138.     while(!done)
  139.     {
  140.         IPXRelinquishControl();
  141.         
  142.         if (!receiveECB.in_use)
  143.         {
  144.             switch(packet_data)
  145.             {
  146.                 case RETURN_KEY :
  147.                                  printf("\n");
  148.                                  break;
  149.                 case ESCAPE_KEY :
  150.                                  done = 1;
  151.                                  break;
  152.                 default :
  153.                                  printf("%c", packet_data);
  154.                                  break;
  155.             }
  156.             IPXListenForPacket( &receiveECB );
  157.         }
  158.         if (kbhit())
  159.         {
  160.             while(sendECB.in_use);
  161.             packet_data = (char) getch();
  162.             switch(packet_data)
  163.             {
  164.                 case RETURN_KEY : printf("\n");
  165.                                   break;
  166.                 case ESCAPE_KEY :
  167.                                   done = 1;
  168.                                   break;
  169.                 default :
  170.                                   printf("%c", packet_data);
  171.                                   break;
  172.             }
  173.             IPXSendPacket( &sendECB );
  174.         }
  175.     }
  176.  
  177.     IPXCancelEvent( &receiveECB );
  178. }
  179.